package com.itheima.Controller;

import com.itheima.Pojo.Clazz;
import com.itheima.Pojo.Result;
import com.itheima.Service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/class")
@CrossOrigin(origins = "*", allowedHeaders = "*", methods = { RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT,
        RequestMethod.DELETE, RequestMethod.OPTIONS })
public class ClazzController {

    private final ClazzService clazzService;  // 声明变量

    @Autowired  // 使用构造器注入
    public ClazzController(ClazzService clazzService) {
        this.clazzService = clazzService;
    }

    @GetMapping
    public Result getAllClass() {
        log.info("查询所有班级信息");
        List allClass = clazzService.getAllClass();
        return Result.success(allClass);
    }

    @DeleteMapping
    public Result deleteById(Integer id) {
        log.info("删除班级号为{}的班级", id);
        clazzService.deleteById(id);
        return Result.success();
    }

    @PutMapping
    public Result updateById(@RequestBody Clazz clazz) {
        log.info("修改班级好号{}的班级", clazz.getClazzId());
        clazzService.updateById(clazz);
        return Result.success();
    }

    @PostMapping
    public Result addClass(@RequestBody Clazz clazz) {
        log.info("添加班级{}", clazz.getClazzName());
        clazzService.addClass(clazz);
        return Result.success();
    }
}
